package com.icebergteam.warfacewidget.data.sql;


import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import com.icebergteam.warfacewidget.AppManager;
import com.icebergteam.warfacewidget.core.LogManager;
import com.icebergteam.warfacewidget.core.utils.ViewUtils;

public class RankTable extends BasicTable{

	public static String className = RankTable.class.getCanonicalName();
	// Constants
	// Table
	public static final String TABLE_NAME	= "rank";
	// Fields
	public static final String FIELD_URL			= "url ";
	public static final String FIELD_HTML			= "html ";
	public static final String FIELD_PUBDATE = "pubdate ";


	public RankTable() {

	}
	
	public String createTableStr() {
		LogManager.LogV(className, "createTable");
	
		String field_url		= FIELD_URL		+ "text";
		String field_html		= FIELD_HTML	+ "text";
		String field_pubdate 	= FIELD_PUBDATE	+ "integer";
			
		return AppManager.getDatabase().createTableStr(TABLE_NAME, field_url, field_html, field_pubdate);
	
	}
	
	public int getCountCurrentUrl() throws SQLException{
		try {
			//FIXME: url - id for request top100
			return AppManager.getDatabase().getRankTable().getCount(String.format("%s = '%s'",RankTable.FIELD_URL, "http://wf.mail.ru/dynamic/ajax/index.php?do=get_ladder_info&server=0&category=1&subcategory=0"
//					"http://wf.mail.ru/dynamic/ajax/index.php?do=get_ladder_info&server=3&category=3&subcategory=1"				
					));
		} catch (Exception e) {
			new SQLException(e.getLocalizedMessage());
			return -1;
			
		}
		
	}
	
//	public Cursor getRank() throws SQLException{
//		try {
//			SQLiteDatabase db = AppManager.getDatabase().getWritableDatabase();
//			if(db != null && db.isOpen()){
//				String mask = "";
//				
//				mask+=String.format("%s = '%s'", RankTable.FIELD_URL, NetworkManager.getUri(new RSSFeedService(), false));
//				LogManager.LogV(RankTable.className, LogManager.getExecuteMethodName(), mask);
//				return db.query(
//					RankTable.TABLE_NAME, 
//					new String[] { RankTable.FIELD_URL, RankTable.FIELD_HTML, RankTable.FIELD_PUBDATE}, 
//					mask,
//					null,
//					null, 
//					null,
//					String.format(" limit %s", AppManager.getNewsSettings().getCountViewNews())
//					);
//			}
//		} catch (Exception e) {
//			LogManager.LogEA(RankTable.className, ViewUtils.getErrorMessage(e));
//		}
//		return null;
//	}
	
		
	public Cursor getRankByUrl(String url) throws SQLException{
		try {
			SQLiteDatabase db = AppManager.getDatabase().getWritableDatabase();
			if(db != null && db.isOpen()){
				return db.query(
					RankTable.TABLE_NAME, 
					new String[] { RankTable.FIELD_URL, RankTable.FIELD_HTML, RankTable.FIELD_PUBDATE}, 
					String.format("%s = ?", RankTable.FIELD_URL), 
					new String[]{url}, 
					null, 
					null,
					null
					);
			}
		} catch (Exception e) {
			LogManager.LogEA(RankTable.className, ViewUtils.getErrorMessage(e));
		}
		return null;
	}
	
	public long getDate(String url){
		try {
			SQLiteDatabase db = AppManager.getDatabase().getWritableDatabase();
			if(db != null && db.isOpen()){
				Cursor cursor = db.query(
					RankTable.TABLE_NAME, 
					new String[] { RankTable.FIELD_PUBDATE}, 
					String.format("%s = ?", RankTable.FIELD_URL), 
					new String[]{url}, 
					null, 
					null,
					null
					);
				
				cursor.moveToFirst();
				return cursor.getLong(0);
				
			}
		} catch (Exception e) {
			LogManager.LogEA(RankTable.className, ViewUtils.getErrorMessage(e));
		}
		return -1;
	}
	
	@Override
	protected WhereWrapper getUpdateWhere(ContentValues cv) {
		WhereWrapper where = new WhereWrapper();
		where.setWhere(String.format("%s=?", RankTable.FIELD_URL));
		where.addValue(cv.getAsString(RankTable.FIELD_URL));
		return where;
	}
	
	@Override
	protected String getTableName() {
		return TABLE_NAME;
	}
}